package com.wzq.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.*;
import com.wzq.service.IAppointmentService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/admin/appointment")
public class AdminAppointmentController extends GetSome{

    @Autowired
    IAppointmentService iAppointmentService;
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        //region 显示条件
        LambdaQueryWrapper<Appointment> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtil.isNotEmpty(query), Appointment::getW_id, query).eq(Appointment::getPass, 0).orderByDesc(Appointment::getPut_time);
        //endregion
        Page<Appointment> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Appointment> pageResult = iAppointmentService.page(page, lqw);
        Map<String,Object> map=new HashMap<>();
        //region       添加用户名称和项目名称
        List<Appointment> appointments=new ArrayList<>();
        for (Appointment appointment : pageResult.getRecords()) {
            if (appointment.getIsover() == 1)        appointment.setOver_c("结束");
            else                                appointment.setOver_c("未结束");
            if (appointment.getPass() == 1)        appointment.setPass_c("通过");
            else                                appointment.setPass_c("未通过");
            if (appointment.getFormat() == 1)        appointment.setFormat_c("按时预约");
            else if(appointment.getFormat() == 2)    appointment.setFormat_c("按天预约");
            else                                     appointment.setFormat_c("按月预约");
            appointment.setW_name( getW_name(appointment.getW_id()) );
            appointment.setP_name( getP_name(appointment.getP_id()) );
            appointments.add(appointment);
        }
        //endregion
        map.put("AppointmentList",appointments);
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    // 申请通过
    @PostMapping("/pass")
    public Result insert(@RequestBody Appointment appointment)   {
        if (appointment.getPass()==1){
            System.out.println("------------------------------------------------");
            System.out.println(getT_type(iProjectService.getById(appointment.getP_id()).getType()));
            if ( "门".equals(getT_type(iProjectService.getById(appointment.getP_id()).getType()))) {
                return new Result(iAppointmentService.updateById(appointment)? Code.SAVE_OK:Code.SAVE_ERR,true,
                        appointment.getPass()==1? "申请已通过":"申请已驳回");
            }
            try {
                //region  判断 是否时间段被预约
                LambdaQueryWrapper<Appointment> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Appointment::getIsover,0).eq(Appointment::getPass,1).eq(Appointment::getP_id,appointment.getP_id());
                // 结束时间不需要 =符号判断  用gt lt 即可
                lqw.and(lq -> lq.ge(Appointment::getBegin_time, appointment.getBegin_time()).le(Appointment::getBegin_time, appointment.getEnd_time())
                        .or().gt(Appointment::getEnd_time, appointment.getBegin_time()).lt(Appointment::getEnd_time, appointment.getEnd_time())
                        .or().le(Appointment::getBegin_time, appointment.getBegin_time()).gt(Appointment::getEnd_time, appointment.getEnd_time()));
                if (iAppointmentService.list(lqw).size()>0)
                    return new Result(Code.SAVE_ERR,false,"该时间段已被预约！");
                //endregion
            } catch (Exception e){
                return new Result(Code.SAVE_ERR,false,"系统繁忙，修改失败！");
            }
        }
        return new Result(iAppointmentService.updateById(appointment)? Code.SAVE_OK:Code.SAVE_ERR,true,
                appointment.getPass()==1? "申请已通过":"申请已驳回");
    }


    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Appointment appointment = iAppointmentService.getById(id);
        //        获得用户名称和项目名称
        appointment.setW_name( getW_name(appointment.getW_id()) );
        appointment.setP_name( getP_name(appointment.getP_id()) );
        Map<String,Object> map=new HashMap<>();
        map.put("appointment",appointment);

        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iAppointmentService.removeById(id);
            return R.ok();
        }

}

